
package cn.itcast.pickpeach;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class PeachActivity extends AppCompatActivity implements View.OnClickListener {
    private  int count = 0;
    private  Button btn_one,btn_two,btn_three,btn_four,btn_five,btn_six,btn_exit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_peach);
        init();
    }
    private void init() {
        //定义点击事件，然后获取统计个数
        btn_one = findViewById(R.id.btn_one);
        btn_two = findViewById(R.id.btn_two);
        btn_three = findViewById(R.id.btn_three);
        btn_four = findViewById(R.id.btn_four);
        btn_five = findViewById(R.id.btn_five);
        btn_six = findViewById(R.id.btn_six);
        btn_exit = findViewById(R.id.btn_exit);

        //点击事件
        btn_one.setOnClickListener(PeachActivity.this);
        btn_two.setOnClickListener(PeachActivity.this);
        btn_three.setOnClickListener(PeachActivity.this);
        btn_four.setOnClickListener(PeachActivity.this);
        btn_six.setOnClickListener(PeachActivity.this);
        btn_five.setOnClickListener(PeachActivity.this);
        btn_exit.setOnClickListener(PeachActivity.this);



    }

    private void info(Button btn){
        count++;
        btn.setVisibility(View.INVISIBLE);
        Toast.makeText(PeachActivity.this,"摘到"+"个桃子",Toast.LENGTH_LONG).show();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_one:
                info(btn_one);
                break;

            case R.id.btn_two:
                info(btn_two);
                break;

            case R.id.btn_three:
                info(btn_three);
                break;

            case R.id.btn_four:
                info(btn_four);
                break;

            case R.id.btn_five:
                info(btn_five);
                break;

            case R.id.btn_six:
                info(btn_six);
                break;

            case R.id.btn_exit:
                info(btn_exit);
                break;
        }

    }

    private  void re() {
        Intent intent = new Intent();
        intent.putExtra("count",count);
        setResult(1,intent);
        //关闭进程
        PeachActivity.this.finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0);
        re();
        return false;
    }
}
